<?php
/**
 * Created by PhpStorm.
 * User: jyolo
 * Date: 2017/3/17
 * Time: 16:17
 */

namespace ReverseModel;

use think\Db;
use FormMaker\FormMaker;

class ReverseModel
{

    static $error = false;
    static $instance = null;
    protected static $fields;
    public static $data;
    public static $preg_tag_map = [
        'label_name' => '/L\[(.*?)\]/',
        'form_type' => '/F\[(\w+)\]/',
//        'choose_option' => '/CO\[(.*?)\]/',
//        'choose_models' => '/CM\[(.*?)\]/',
//        'relation_field' => '/RF\[(.*?)\]/',
//        'validata_rule' => '/VR\[(.*?)\]/',
//        'validate_message' => '/VM\[(.*?)\]/',
//        'validate_scene' => '/VS\[(.*?)\]/',
//        'is_multi' => '/IM\[(.*?)\]/',
//        'is_search' => '/IS\[(.*?)\]/',
    ];
//    protected static $componet = [
//        ['type'=>'text','name'=>'','label'=>'单行文本','helpinfo'=>'帮助信息','class'=>'text preview','value'=>''],
//        ['type'=>'number','name'=>'','label'=>'数字输入','helpinfo'=>'帮助信息','class'=>'number preview','value'=>''],
//        ['type'=>'password','name'=>'','label'=>'数字输入','helpinfo'=>'帮助信息','class'=>'number preview','value'=>''],
//        ['type'=>'textarea','name'=>'','label'=>'多行文本','helpinfo'=>'帮助信息','class'=>'textarea preview','value'=>''],
//        ['type'=>'editor','name'=>'','label'=>'文本编辑器','helpinfo'=>'帮助信息','class'=>'editor preview','value'=>''],
//        ['type'=>'checkbox','name'=>'','label'=>'复选框','helpinfo'=>'帮助信息','class'=>'checkbox preview','value'=>['data'=>['0' => '选项1','1'=>'选项2']]],
//        ['type'=>'radio','name'=>'','label'=>'单选框','helpinfo'=>'帮助信息','class'=>'radio preview','value'=>['data'=>['0' => '选项1','1'=>'选项2']]],
//        ['type'=>'select','name'=>'','label'=>'下拉选择','helpinfo'=>'普通的下拉选择','class'=>'select preview','value'=>['data'=>['0' => '选项1','1'=>'选项2']]],
//        ['type'=>'tree','name'=>'','label'=>'下拉层级树','helpinfo'=>'层级关系的数据,字段一般为parentid或者pid','class'=>'tree preview','value'=>['data'=> 'tree_example_data','setting'=>['option'=>'name','value'=>'id']]],
//        ['type'=>'relation','name'=>'','label'=>'模型关联数据','helpinfo'=>'关联其它模型的数据','class'=>'relation preview','value'=>['data'=> ['0' => '数据选项','1'=>'数据选项']]],
//        ['type'=>'images','name'=>'','label'=>'图片上传','helpinfo'=>'帮助信息','class'=>'images preview','value'=>''],
//        ['type'=>'files','name'=>'','label'=>'文件上传','helpinfo'=>'帮助信息','class'=>'files preview','value'=>''],
//    ];

    public static function init($table){
        if(self::$instance == null){
            self::$instance = new self;
        }
        self::$fields = self::get_field_info($table);

        return self::$instance;
    }

    protected static function get_field_info($table){
        $sql = 'show full fields from '.$table;
        $field = Db::query($sql);
        return $field;
    }
    protected function comment_to_data(){
        $data = [];
        foreach(self::$fields as $k => $v){

            if($v['Key'] == 'PRI')continue;
            foreach (self::$preg_tag_map as $sk => $sv){
                $reg = preg_match($sv,$v['Comment'],$match);

                if(isset($match[1]) && $match[1]){

                    $data[$k][$sk] = trim($match[1],' ');
                    $data[$k]['field_name'] = $v['Field'];
                    $data[$k]['field_type'] = preg_replace('/\(\d+\)/','',$v['Type']);
                    preg_match('/(\d+)/',$v['Type'],$match);
                    $data[$k]['field_length'] = $match[1];


                    if($v['Default'] != null) $data[$k]['field_defualt_value'] = $v['Default'];

                }

            }
        }


        //重新排序从0开始
        foreach($data as $k => $v){
            self::$data[] = $v;
        }

        //self::$data = $data;
        return self::$data;
    }

    public function get_componet(){
        $data = $this->comment_to_data();
        if(!count($data)) return false;

        $componet = '';


        foreach($data as $k => $v){
            $helpinfo = (isset($v['helpinfo']) && $v['helpinfo']) ?$v['helpinfo'] : '';
            $class = $v['form_type'].' preview '.$v['field_name'];

            $dom_plan_class = FormMaker::get_dom_plan();



            $componet_group = $dom_plan_class::$componet_group;



            if(in_array($v['form_type'] ,$componet_group['choose']['componet'])){
                $value['data'] = [
                    ['v'=> 0 ,'n' => '选项一'],
                    ['v'=> 1 ,'n' => '选项二'],
                ];
                $value['setting'] = ['option' => 'n' ,'value' => 'v'];
            }else{
                $value = '';
            }
            
            $componet .= FormMaker::build($v['form_type'])
                ->label($v['label_name'] )
                ->helpinfo($helpinfo)
                ->class($class)
                ->value($value)
                ->render();
        }
        return $componet;
    }




}